#include <Windows.h>
#include <CommCtrl.h>
#include "../Application/Application.h"
#include "../Layout/HorizontalLayout.h"
#include "../Layout/VerticalLayout.h"
#include "Tab.h" 

namespace GUI
{
	namespace Win32
	{

		Tab::Tab() : m_iWidth(-1), m_iHeight(-1)
		{
			m_iId = Application::GetId();
			Application::RegisterWidget(this);
		}

		Tab::Tab(int _iWidth, int _iHeight) : m_iWidth(_iWidth), m_iHeight(_iHeight)
		{
			m_iId = Application::GetId();
			Application::RegisterWidget(this);
		}

		Tab::~Tab()
		{
			Application::UnregisterWidget(this);
		}

		void Tab::Create(HWND _hWindow, HINSTANCE _hInstance)
		{
			m_hInstance = _hInstance;
			m_hWnd = _hWindow;
			m_hTabCtrl = CreateWindowEx(NULL, WC_TABCONTROL, NULL, WS_CHILD | WS_VISIBLE, GetX(), GetY(), GetWidth(), GetHeight(), _hWindow, (HMENU)m_iId, _hInstance, NULL);
		}

		Core::GUI::WidgetType Tab::GetType() const
		{
			return Core::GUI::WidgetType::Tab;
		}

		int Tab::GetId() const
		{
			return m_iId;
		}

		void Tab::Accept(Core::GUI::IVerticalLayout * _pVerticalLayout)
		{
			((VerticalLayout*)_pVerticalLayout)->Visit(this);
		}

		void Tab::Accept(Core::GUI::IHorizontalLayout * _pHorizontalLayout)
		{
			((HorizontalLayout*)_pHorizontalLayout)->Visit(this);
		}
		
		int Tab::GetWidth() const
		{
			return m_iWidth;
		}

		int Tab::GetHeight() const
		{
			return m_iHeight;
		}

		int Tab::GetX() const
		{
			return m_iX;
		}

		int Tab::GetY() const
		{
			return m_iY;
		}

		void Tab::Resize(int _iWidth, int _iHeight)
		{
			m_iWidth = _iWidth;
			m_iHeight = _iHeight;
			MoveWindow(m_hTabCtrl, m_iX, m_iY, _iWidth, _iHeight, true);
		}

		void Tab::Move(int _x, int _y)
		{
			m_iX = _x;
			m_iY = _y;
			MoveWindow(m_hTabCtrl, _x, _y, m_iWidth, m_iHeight, true);
		}

		void Tab::AddTab(const std::string & _strLabel, Core::GUI::IVerticalLayout * _pLayout)
		{
			TCITEM tie;
			tie.mask = TCIF_TEXT | TCIF_IMAGE;
			tie.iImage = -1;
			tie.pszText = const_cast<char*>(_strLabel.c_str());
			TabCtrl_InsertItem(m_hTabCtrl, m_hTabs.size(), &tie);


			RECT tab_rectangle;
			GetClientRect(m_hTabCtrl, &tab_rectangle);
			SendMessage(m_hTabCtrl, TCM_ADJUSTRECT, FALSE, (LPARAM)&tab_rectangle);

			int id = Application::GetId();
			auto htabCtrl = CreateWindowEx(NULL, "STATIC", NULL, WS_CHILD | WS_VISIBLE | SS_OWNERDRAW, tab_rectangle.left, tab_rectangle.top,
				tab_rectangle.right - tab_rectangle.left,
				tab_rectangle.bottom - tab_rectangle.top,
				m_hTabCtrl, (HMENU)id, m_hInstance, NULL);

			//SetParent(htabCtrl, m_hWnd);
			m_hTabs.push_back(htabCtrl);

			((VerticalLayout*)_pLayout)->m_hWnd = htabCtrl;
			((VerticalLayout*)_pLayout)->m_hInstance = m_hInstance;
			((VerticalLayout*)_pLayout)->m_iWidth = tab_rectangle.right - tab_rectangle.left;
			((VerticalLayout*)_pLayout)->m_iHeight = tab_rectangle.bottom - tab_rectangle.top;
			((VerticalLayout*)_pLayout)->m_iX = tab_rectangle.left;
			((VerticalLayout*)_pLayout)->m_iY = tab_rectangle.top;
		}

		void Tab::AddTab(const std::string & _strLabel, Core::GUI::IHorizontalLayout * _pLayout)
		{
			TCITEM tie;
			tie.mask = TCIF_TEXT | TCIF_IMAGE;
			tie.iImage = -1;
			tie.pszText = const_cast<char*>(_strLabel.c_str());
			TabCtrl_InsertItem(m_hTabCtrl, m_hTabs.size(), &tie);


			RECT tab_rectangle;
			GetClientRect(m_hTabCtrl, &tab_rectangle);
			SendMessage(m_hTabCtrl, TCM_ADJUSTRECT, FALSE, (LPARAM)&tab_rectangle);

			int id = Application::GetId();
			auto htabCtrl = CreateWindowEx(NULL, "STATIC", NULL, WS_CHILD | WS_VISIBLE | SS_OWNERDRAW, tab_rectangle.left, tab_rectangle.top,
				tab_rectangle.right - tab_rectangle.left,
				tab_rectangle.bottom - tab_rectangle.top,
				m_hTabCtrl, (HMENU)id, m_hInstance, NULL);

			//SetParent(htabCtrl, m_hWnd);
			m_hTabs.push_back(htabCtrl);

			((HorizontalLayout*)_pLayout)->m_hWnd = htabCtrl;
			((HorizontalLayout*)_pLayout)->m_hInstance = m_hInstance;
			((HorizontalLayout*)_pLayout)->m_iWidth = tab_rectangle.right - tab_rectangle.left;
			((HorizontalLayout*)_pLayout)->m_iHeight = tab_rectangle.bottom - tab_rectangle.top;
			((HorizontalLayout*)_pLayout)->m_iX = tab_rectangle.left;
			((HorizontalLayout*)_pLayout)->m_iY = tab_rectangle.top;
		}

		void Tab::RemoveTab(const std::string & _strLabel)
		{

		}

		void Tab::ShowTab(int index)
		{
			for (uint32 i = 0; i < m_hTabs.size(); ++i)
			{
				if (i == index)
				{
					ShowWindow(m_hTabs[i], SW_SHOW);
				}
				else
				{
					ShowWindow(m_hTabs[i], SW_HIDE);
				}
			}
		}
	}
}